using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
	[Serializable()]
	public class ThietDatBO : Csla.BusinessBase<ThietDatBO>
	{
		#region Business Properties
		
		private int? thietDatID = null;
		[System.ComponentModel.DataObjectField(true, true, false)]
		public int? ThietDatID
		{
			get { return this.thietDatID; }
			set { this.thietDatID = value; PropertyHasChanged("ThietDatID"); }
		}
		
		private string loaiThietDat = null;
		[System.ComponentModel.DataObjectField(false, false, true)]
		public string LoaiThietDat
		{
			get { return this.loaiThietDat; }
			set { this.loaiThietDat = value; PropertyHasChanged("LoaiThietDat"); }
		}
		
		private string giaTri = null;
		[System.ComponentModel.DataObjectField(false, false, true)]
		public string GiaTri
		{
			get { return this.giaTri; }
			set { this.giaTri = value; PropertyHasChanged("GiaTri"); }
		}
		
		private string ghiChu = null;
		[System.ComponentModel.DataObjectField(false, false, true)]
		public string GhiChu
		{
			get { return this.ghiChu; }
			set { this.ghiChu = value; PropertyHasChanged("GhiChu"); }
		}
		
		private bool? deleted = false;
		[System.ComponentModel.DataObjectField(false, false, true)]
		public bool? Deleted
		{
			get { return this.deleted; }
			set { this.deleted = value; PropertyHasChanged("Deleted"); }
		}
		
		private bool? checkedColumn = false;
		public bool? CheckedColumn
		{
			get { return checkedColumn; }
			set { checkedColumn = value; PropertyHasChanged("CheckedColumn"); }
		}
		
		private int? sTT0 = 0;
		public int? STT0
		{
			get { return sTT0; }
			set { sTT0 = value; PropertyHasChanged("sTT0"); }
		}
		
		protected override object GetIdValue()
		{
			return thietDatID.Value;
		}
		#endregion Business Properties
		//==========================================================================
		
		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}
		
		public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }
		
		private static int newThietDatID =  -1;
		private ThietDatBO()
		{
			thietDatID = newThietDatID--;
		}

		public ThietDatBO(SqlDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public ThietDatBO(SqlDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
                MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static ThietDatBO NewRootThietDatBO()
		{
			return DataPortal.Create<ThietDatBO>(new RootCriteria());
		}

		internal static ThietDatBO NewChildThietDatBO()
		{
			return DataPortal.Create<ThietDatBO>(new ChildCriteria());
		}

		public static ThietDatBO GetRootThietDatBO(int thietDatID)
		{
			return DataPortal.Fetch<ThietDatBO>(new RootCriteria(thietDatID));
		}

		public static ThietDatBO GetRootThietDatBO(string where, bool isWhere)
		{
			return DataPortal.Fetch<ThietDatBO>(where);
		}

		internal static ThietDatBO GetChildThietDatBO(SqlDataReader dataReader)
		{
			return new ThietDatBO(dataReader);
		}

		public static void DeleteThietDatBO(int thietDatID)
		{
			DataPortal.Delete(new RootCriteria(thietDatID));
		}

		public static void DeleteThietDatBO(int thietDatID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(thietDatID, directDelete));
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int ThietDatID;
			public bool directDelete = false;
			public RootCriteria(int thietDatID)
			{
				this.ThietDatID = thietDatID;
				this.directDelete = false;
			}
			
			public RootCriteria(int thietDatID, bool directDelete)
            {
                this.ThietDatID = thietDatID;
                this.directDelete = directDelete;
            }
            
			public RootCriteria() {}
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int ThietDatID;
			public bool directDelete = false;
			public ChildCriteria(int thietDatID)
			{
				this.ThietDatID = thietDatID;
				this.directDelete = false;
			}
			
			public ChildCriteria(int thietDatID, bool directDelete)
            {
                this.ThietDatID = thietDatID;
                this.directDelete = directDelete;
            }
            
			public ChildCriteria() {}
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}
		
		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThietDatBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[ThietDatID] = " + criteria.ThietDatID);
			try
			{
				using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
				{
					if (dataReader.Read())
						Fetch(dataReader);                    
				}
			}
			catch (SqlException)
			{
				throw;
			}
			catch (Exception)
			{
				throw;
			}
			finally{}
		}

		private void This_Fetch(string where)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThietDatBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
			try
			{
				using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
				{
					if (dataReader.Read())
						Fetch(dataReader);                    
				}
			}
			catch (SqlException)
			{
				throw;
			}
			catch (Exception)
			{
				throw;
			}
			finally{}
		}

		private void Fetch(SqlDataReader dataReader)
		{
			if (!(dataReader["ThietDatID"] is DBNull))
				this.thietDatID = (int)dataReader["ThietDatID"];
			if (!(dataReader["LoaiThietDat"] is DBNull))
				this.loaiThietDat = (string)dataReader["LoaiThietDat"];
			if (!(dataReader["GiaTri"] is DBNull))
				this.giaTri = (string)dataReader["GiaTri"];
			if (!(dataReader["GhiChu"] is DBNull))
				this.ghiChu = (string)dataReader["GhiChu"];
			if (!(dataReader["Deleted"] is DBNull))
				this.deleted = (bool)dataReader["Deleted"];
			
		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThietDatBO_InsertBO");
			db.AddInParameter(cmd, "@LoaiThietDat", SqlDbType.NVarChar, this.loaiThietDat);
			db.AddInParameter(cmd, "@GiaTri", SqlDbType.NVarChar, this.giaTri);
			db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this.ghiChu);
			db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this.deleted);
			db.AddOutParameter(cmd, "@NewThietDatID", SqlDbType.Int, 10);
			try
			{
				db.ExecuteNonQuery(cmd);
				this.thietDatID = (int)cmd.Parameters["@NewThietDatID"].Value;
			}
			catch (SqlException)
			{
				throw;
			}
			catch (Exception)
			{
				throw;
			}
			finally{}
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThietDatBO_UpdateBO");
			db.AddInParameter(cmd, "@ThietDatID", SqlDbType.Int, this.thietDatID);
			db.AddInParameter(cmd, "@LoaiThietDat", SqlDbType.NVarChar, this.loaiThietDat);
			db.AddInParameter(cmd, "@GiaTri", SqlDbType.NVarChar, this.giaTri);
			db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this.ghiChu);
			db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this.deleted);
			
			try
			{
				db.ExecuteNonQuery(cmd);
			}
			catch (SqlException)
			{
				throw;
			}
			catch (Exception)
			{
				throw;
			}
			finally{}
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(thietDatID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThietDatBO_DeleteBO");
			db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
			db.AddInParameter(cmd, "@ThietDatID", SqlDbType.Int, criteria.ThietDatID);
			try
			{
				db.ExecuteNonQuery(cmd);
			}
			catch (SqlException)
			{
				throw;
			}
			catch (Exception)
			{
				throw;
			}
			finally{}
		}
		#endregion Delete
		//==========================================================================

		#region Internal method
		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(thietDatID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}
